﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuicksandCRUD;
using TestAPP.TestObjects;

namespace TestAPP
{

    public partial class Form1 : Form
    {
        private ContextMenu _cmPersonTopLevelTree { get; set; }
        private MenuItem _miPersonNew { get; set; }
        ContextMenu _cmPersonTree { get; set; }
        private MenuItem _miPersonDelete { get; set; }

        ContextMenu _cmCompanyTopLevelTree { get; set; }
        private MenuItem _miCompanyNew { get; set; }
        ContextMenu _cmCompanyTree { get; set; }
        private MenuItem _miCompanyDelete { get; set; }

        ContextMenu _cmEventTopLevelTree { get; set; }
        private MenuItem _miEventNew { get; set; }
        ContextMenu _cmEventTree { get; set; }
        private MenuItem _miEventDelete { get; set; }

        private TreeNode _tnPerson { get; set; }
        private TreeNode _tnCompany { get; set; }
        private TreeNode _tnEvent { get; set; }

        private List<Person> _personList;
        private List<Company> _companyList;
        private List<Event> _eventList;

        private ToolBarButton _tbbAdd;
        private ToolBarButton _tbbDelete;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1Load(object sender, EventArgs e)
        {
            BuildContextMenus();
            LoadTreeNodes();

            
            _tbbAdd = new ToolBarButton("Add") {Enabled = false};
            _tbbDelete = new ToolBarButton("Delete") { Enabled = false };
            toolBar1.ButtonClick += new ToolBarButtonClickEventHandler(toolBar1_ButtonClick);
            toolBar1.Buttons.Add(_tbbAdd);
            toolBar1.Buttons.Add(_tbbDelete);
        }

        void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            if(e.Button == _tbbAdd && _tbbAdd.Enabled==true)
            {
                if (treeView1.SelectedNode != null)
                {
                    if (treeView1.SelectedNode.Tag is Person || treeView1.SelectedNode.Text.Contains("People"))
                    {
                        AddPerson();
                    }
                    else if (treeView1.SelectedNode.Tag is Event || treeView1.SelectedNode.Text.Contains("Event"))
                    {
                       AddEvent();
                    }
                    else if (treeView1.SelectedNode.Tag is Company || treeView1.SelectedNode.Text.Contains("Company"))
                    {
                       AddCompany();
                    }

                    UpdateTopLevel();
                }
            }
            else if (e.Button == _tbbDelete && _tbbDelete.Enabled == true)
            {
                if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag is ResourceBase)
                {
                    if (treeView1.SelectedNode.Tag is Person)
                    {
                        DeletePerson();
                    }
                    else if (treeView1.SelectedNode.Tag is Event)
                    {
                        DeleteEvent();
                    }
                    else if (treeView1.SelectedNode.Tag is Company)
                    {
                       DeleteCompany();
                    }
                }
            }
        }

        private void AssignMenus()
        {
            _cmEventTopLevelTree = new ContextMenu();
            _cmEventTree = new ContextMenu();

            _cmEventTopLevelTree.MenuItems.Add(_miEventNew);
            _cmEventTree.MenuItems.Add(_miEventDelete);

            _cmCompanyTopLevelTree = new ContextMenu();
            _cmCompanyTree = new ContextMenu();

            _cmCompanyTopLevelTree.MenuItems.Add(_miCompanyNew);
            _cmCompanyTree.MenuItems.Add(_miCompanyDelete);


            _cmPersonTopLevelTree = new ContextMenu();
            _cmPersonTree = new ContextMenu();

            _cmPersonTopLevelTree.MenuItems.Add(_miPersonNew);
            _cmPersonTree.MenuItems.Add(_miPersonDelete);
        }

        

        private void LoadTreeNodes()
        {

            LoadPersonList();
            LoaCompanyList();
            LoadEventList();
            
            _tnPerson = new TreeNode("People List - " + _personList.Count + " Total");
            _tnCompany = new TreeNode("Company List - " + _personList.Count + " Total");
            _tnEvent = new TreeNode("Event List - " + _personList.Count + " Total");

            _tnPerson.Tag = _personList;
            _tnCompany.Tag = _companyList;
            _tnEvent.Tag = _eventList;

            treeView1.Nodes.Add(_tnPerson);
            treeView1.Nodes.Add(_tnCompany);
            treeView1.Nodes.Add(_tnEvent);

            _tnPerson.ContextMenu = _cmPersonTopLevelTree;
            _tnCompany.ContextMenu = _cmCompanyTopLevelTree;
            _tnEvent.ContextMenu = _cmEventTopLevelTree;
            

            foreach (Person item in _personList)
            {
                _tnPerson.Nodes.Add(GetPersonNode(item));
            }

            foreach (Company item in _companyList)
            {
                _tnCompany.Nodes.Add(GetCompanyNode(item));
            }
            foreach (Event item in _eventList)
            {
                _tnEvent.Nodes.Add(GetEventNode(item));
            }

            
        }

        private void LoadPersonList()
        {
            ICollection<ResourceBase> collection = InMemoryRepositorySingleton.Instance.GetAll(new Person());
            _personList = new List<Person>();
            foreach(Person item in collection)
            {
                _personList.Add(item);
            }
        }

        private void LoaCompanyList()
        {
            ICollection<ResourceBase> collection = InMemoryRepositorySingleton.Instance.GetAll(new Company());
            _companyList = new List<Company>();
            foreach (Company item in collection)
            {
                _companyList.Add(item);
            }
        }


        private void LoadEventList()
        {
            ICollection<ResourceBase> collection = InMemoryRepositorySingleton.Instance.GetAll(new Event());
            _eventList = new List<Event>();
            foreach (Event item in collection)
            {
                _eventList.Add(item);
            }
        }
         
         
        private void BuildContextMenus()
        {
            CreatePersonMenu();
            CreateCompanyMenu();
            CreateEventMenu();
            AssignMenus();
        }

        private void CreateEventMenu()
        {
            _cmEventTopLevelTree = new ContextMenu();
            _cmEventTree = new ContextMenu();

            _miEventNew = new MenuItem("Add New Event");
            _miEventNew.Click += MiEventNewClick;

            _miEventDelete = new MenuItem("Delete This Event");
            _miEventDelete.Click += MiEventDeleteClick;
        }

        void MiEventNewClick(object sender, EventArgs e)
        {
            AddEvent();
        }

        private void AddEvent()
        {
            TreeNode tn = GetEventNode(new Event {EventName ="New Event"});
            _tnEvent.Nodes.Add(tn);
        }

        void MiEventDeleteClick(object sender, EventArgs e)
        {
            DeleteEvent();
        }

        private void DeleteEvent()
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag is Event)
            {
                ((Event)treeView1.SelectedNode.Tag).Delete();
                treeView1.SelectedNode.Remove();
                UpdateTopLevel();
            }
        }

        private void CreateCompanyMenu()
        {
            _cmCompanyTopLevelTree = new ContextMenu();
            _cmCompanyTree = new ContextMenu();

            _miCompanyNew = new MenuItem("Add New Person");
            _miCompanyNew.Click += MiCompanyNewClick;
           

            _miCompanyDelete = new MenuItem("Delete This Person");
            _miCompanyDelete.Click += MiCompanyDeleteClick;

        }

        void MiCompanyDeleteClick(object sender, EventArgs e)
        {
            DeleteCompany();
        }

        private void DeleteCompany()
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag is Company)
            {
                ((Company)treeView1.SelectedNode.Tag).Delete();
                treeView1.SelectedNode.Remove();
                UpdateTopLevel();
            }
        }

        void MiCompanyNewClick(object sender, EventArgs e)
        {
            AddCompany();
        }

        private void AddCompany()
        {
            TreeNode tn = GetCompanyNode(new Company { CompanyName = "New Compnay" });
            _tnCompany.Nodes.Add(tn);
        }

        private void CreatePersonMenu()
        {
            _cmPersonTopLevelTree = new ContextMenu();
            _cmPersonTree = new ContextMenu();

            _miPersonNew = new MenuItem("Add New Person");
            _miPersonNew.Click += MiPersonNewClick;      
            

            _miPersonDelete = new MenuItem("Delete This Person");
            _miPersonDelete.Click += MiPersonDeleteClick;

        }

        void MiPersonDeleteClick(object sender, EventArgs e)
        {
            DeletePerson();
        }

        private void DeletePerson()
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag is Person)
            {
                ((Person)treeView1.SelectedNode.Tag).Delete();
                treeView1.SelectedNode.Remove();
                UpdateTopLevel();
            }
        }

        void MiPersonNewClick(object sender, EventArgs e)
        {
            AddPerson();
        }

        private void AddPerson()
        {
            TreeNode tn = GetPersonNode(new Person { FirstName = "New Person" });
            _tnPerson.Nodes.Add(tn);
        }


        private TreeNode GetCompanyNode(Company item)
        {

            if (!_companyList.Contains(item))
            {
                _companyList.Add(item);
            }

            item.Save();
            TreeNode tn = new TreeNode(item.ToString()) {ContextMenu = _cmCompanyTree, Tag = item};
            return tn;
        }

        

        private TreeNode GetEventNode(Event item)
        {
            if (!_eventList.Contains(item))
            {
                _eventList.Add(item);
            }

            item.Save();
            TreeNode tn = new TreeNode(item.ToString()) {ContextMenu = _cmEventTree, Tag = item};
            return tn;
        }

        private TreeNode GetPersonNode(Person item)
        {
            if (!_personList.Contains(item))
            {
                _personList.Add(item);
            }


            item.Save();
            TreeNode tn = new TreeNode(item.ToString()) {ContextMenu = _cmPersonTree, Tag = item};
            return tn;
        }

        private void TreeView1BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag is ResourceBase)
            {
                if (treeView1.SelectedNode.Tag is Person)
                {
                    ((Person)treeView1.SelectedNode.Tag).Save();
                    treeView1.SelectedNode.Text = ((Person)treeView1.SelectedNode.Tag).ToString();
                }
                else if (treeView1.SelectedNode.Tag is Event)
                {
                    ((Event)treeView1.SelectedNode.Tag).Save();
                    treeView1.SelectedNode.Text = ((Event)treeView1.SelectedNode.Tag).ToString();
                }
                else if (treeView1.SelectedNode.Tag is Company)
                {
                    ((Company)treeView1.SelectedNode.Tag).Save();
                    treeView1.SelectedNode.Text = ((Company)treeView1.SelectedNode.Tag).ToString();
                }
            }
        }

        private void TreeView1AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag is ResourceBase)
            {
                propertyGrid1.SelectedObject = treeView1.SelectedNode.Tag;
                propertyGrid1.Update();
            }
            else
            {
                propertyGrid1.SelectedObject = null;
                propertyGrid1.Update();
            }

            UpdateTopLevel();

            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag is Person || treeView1.SelectedNode.Text.Contains("People"))
                {
                    _tbbAdd.Enabled = true;
                    _tbbAdd.Text = @"Add Person";

                    if (treeView1.SelectedNode.Tag is Person)
                    {
                        _tbbDelete.Enabled = true;
                        _tbbDelete.Text = @"Delete " + ((Person)treeView1.SelectedNode.Tag).ToString();
                    }
                }
                else if (treeView1.SelectedNode.Tag is Event || treeView1.SelectedNode.Text.Contains("Event"))
                {
                    _tbbAdd.Enabled = true;
                    _tbbAdd.Text = @"Add Event";

                    if (treeView1.SelectedNode.Tag is Event)
                    {
                        _tbbDelete.Enabled = true;
                        _tbbDelete.Text = @"Delete " + ((Event)treeView1.SelectedNode.Tag).ToString();
                    }
                }
                else if (treeView1.SelectedNode.Tag is Company || treeView1.SelectedNode.Text.Contains("Company"))
                {
                    _tbbAdd.Enabled = true;
                    _tbbAdd.Text = @"Add Company";

                    if (treeView1.SelectedNode.Tag is Company)
                    {
                        _tbbDelete.Enabled = true;
                        _tbbDelete.Text = @"Delete " + ((Company)treeView1.SelectedNode.Tag).ToString();
                    }
                }
            }
            else
            {
                _tbbAdd.Enabled = false;
                _tbbDelete.Enabled = false;
            }
        }

        private void UpdateTopLevel()
        {
            _tnPerson.Text = @"People List - " + _personList.Count + @" Total";
            _tnCompany.Text = @"Company List - " + _companyList.Count + @" Total";
            _tnEvent.Text = @"Event List - " + _eventList.Count + @" Total";
        }
    }
}